package com.uplooking.controller;

import com.alibaba.fastjson.JSON;
import com.uplooking.dao.DataSourceUtils;
import com.uplooking.domain.UserInfoResponseBean;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


public class LoginController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType ("application/json;charset=utf8");

        String username = req.getParameter ("username");
        String password = req.getParameter ("password");

        QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource ());
        try {
            UserInfoResponseBean userInfoResponseBean = new UserInfoResponseBean();
            Object[] i = (Object[]) queryRunner.query("select * from userinfo where username=? and password=?",new ArrayHandler(),username,password);
            if (i.length > 0) {
                userInfoResponseBean.setCode (0);
                userInfoResponseBean.setMsg ("登录成功");
            } else {
                userInfoResponseBean.setCode (1);
                userInfoResponseBean.setMsg ("登录失败");
            }
            resp.getWriter ().write (JSON.toJSONString (userInfoResponseBean));
        } catch (SQLException e) {
            e.printStackTrace ();
            System.out.println ("查询异常..");
        }

    }
}
